/**
 * 游戏主场景
 * 包含游戏场景初始化过程
 * 电脑角色、玩家角色初始化
 * 游戏回合控制
 */



function Game(){
	this.autoRole = new AutoRole("干女儿");
	this.gameRole = new GameRole("干爹");
	this.currentHander = this.autoRole;
	//本局游戏当前喊出的值
	this.currentHandValue = [1,1];
	//历史记录
	this.handHistory = new Array();
	this.preLoser = this.autoRole;
	this.diceImageArr = ["images/s1_1.png","images/s2_1.png","images/s3_1.png","images/s4_1.png","images/s5_1.png","images/s6_1.png"];
}

Game.prototype.log = function(msg){
	console.log(msg);
};

Game.prototype.getWords = function(a,b){
	return "喊"+a+"个<img src=\""+this.diceImageArr[b-1]+"\"/>";
};

Game.prototype.showHP = function(){
	
	UI.showCpLife(this.autoRole.hp);
	UI.showUpLife(this.gameRole.hp);
};
/**
 * 每个开后判断输赢,并且根据当前喊出的角色来展示不同的结果场面
 * 并且减少血值
 */
Game.prototype.judgeDiceAndCutdonwHP = function(){
	var count=0;
	for(var i=0;i<5;i++){//判断电脑的骰子数值
		if(this.autoRole.diceArr[i] == this.currentHandValue[1]){
			count++;
		}
	}
	for(var i=0;i<5;i++){//判断玩家的骰子数值
		if(this.gameRole.diceArr[i] == this.currentHandValue[1]){
			count++;
		}
	}
	
	
	if(count>=this.currentHandValue[0]){//对方喊对了，自己减血
		this.currentHander.cutdownHP();
		this.preLoser = this.currentHander;
		this.log("autoRoleHp:"+this.autoRole.hp+"\tgameRoleHP:"+this.gameRole.hp);
		this.showHP();
		if(this.currentHander==this.autoRole){//电脑开
			UI.showTip("cry");
		}else{
			UI.showTip("happy");
		}
		return false;//没有开对
	}else{//对方减血
		if(this.currentHander==this.autoRole){//电脑开
			this.gameRole.cutdownHP();
			this.preLoser = this.gameRole;
			UI.showTip("happy");
		}else{
			UI.showTip("cry");
			this.autoRole.cutdownHP();
			this.preLoser = this.autoRole;
		}
		this.log("autoRoleHp:"+this.autoRole.hp+"\tgameRoleHP:"+this.gameRole.hp);
		this.showHP();
		return true;//开对了
	}
	
};
/*
 * 通过血值判断游戏本局是不是结束
 * 如果结束展示最终场景
 */
Game.prototype.judgetHPAndShowResult = function(){
	if(this.autoRole.hp<=0){
		//展示玩家赢了场景
		this.log("玩家赢了");
		this.preLoser = this.autoRole;
		UI.showResult("win");
	}else if(this.gameRole.hp<=0){
		//展示玩家输了的场景
		this.log("电脑赢了");
		this.preLoser = this.gameRole;
		UI.showResult("lost");
	}else{//都还有剩血
		setTimeout(function(){
			game.play();
		},3000);
	}
};

//展示开始时的场景,新一局游戏开始
Game.prototype.init = function(){
	this.currentHander = this.autoRole;
	this.currentHandValue = [0,1];
	this.handHistory = new Array();
	game.gameRole.hp = 3;
	game.autoRole.hp = 3;
	console.log("初始化");
};
//电脑喊了之后如果还没有结束,轮到玩家喊，等待玩家喊
Game.prototype.waitGameRoleHand = function(){
	//TODO 启刚实现
	//在玩家点击确定后调用设置gameRole的handValue
	//并且调用this.playOne();
	UI.shwoSelectRoll();
	UI.record(game.autoRole.name,"干爹该你了哦。。");
	console.log("等待玩家喊。。。");
};

//一次喊值回合
Game.prototype.playOne = function(){
	if(!this.currentHander.hand()){//如果在喊的过程中没有开(电脑玩家，游戏玩家hand和open分开实现),游戏玩家喊默认返回false
		this.handHistory.push([this.currentHander.name,this.currentHandValue[0],this.currentHandValue[1]]);
		if(this.handHistory.length>15){//如果超过15条则删除第一条
			this.handHistory.pop();
		}
		if(this.currentHander instanceof GameRole){
			this.currentHander = this.autoRole;
			this.playOne();
		}else{
			this.currentHander = this.gameRole;
			this.waitGameRoleHand();
		}
	}else{
		this.autoRole.open();
	}
};

//点击开始后进入游戏场景，摇骰子和
Game.prototype.play = function(){
	this.autoRole.init();
	this.gameRole.init();
	UI.showRoll();
	this.showHP();
	UI.clearRound();
	setTimeout(function(){
		game.currentHander = game.preLoser;
		if(game.preLoser instanceof GameRole){//新一轮开始，如果上次是玩家掉血，那么等待玩家第一次喊
			game.waitGameRoleHand();
		}else{
			game.playOne();
		}
	},3000);
};
